package com.auth.service.impl;

import com.auth.entity.dao.Role;
import com.auth.entity.dao.User;
import com.auth.entity.dao.UserRole;
import com.auth.entity.dto.UserDto;
import com.auth.entity.vo.UserVo;
import com.auth.exception.AuthException;
import com.auth.mapper.UserMapper;
import com.auth.service.IRoleService;
import com.auth.service.IUserRoleService;
import com.auth.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyCode.feature.mybaits.CustomPage;
import io.seata.core.context.RootContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.auth.entity.factory.UserFactory.USER_CONVERTER;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final IUserRoleService iUserRoleService;
    private final IRoleService iRoleService;

    @Override
    public Integer saveUser(UserVo userVo) {
        User user = USER_CONVERTER.toUser(userVo);
        save(user);
        return user.getId();
    }

    @Override
    public UserDto getUserById(Integer id) {
        User user = getById(id);
        return USER_CONVERTER.toUserDto(user);
    }

    @Override
    public List<UserDto> listUser(CustomPage customPage) {
        List<User> userList = CustomPage.getResult(customPage, new User(), this, null);
        return USER_CONVERTER.toListUserDto(userList);
    }

    /**
     * 同时需要用户角色表中当前用户关联的记录
     */
    @Transactional
    @Override
    public void delUserById(Integer id) {
        removeById(id);
        iUserRoleService.delByUserId(id);
    }

    @Override
    public void enableUser(Integer id) {
        updateById(User.builder().id(id).enabled(1).build());
    }

    @Override
    public void disableUser(Integer id) {
        updateById(User.builder().id(id).enabled(0).build());
    }

    @Transactional
    @Override
    public void registerUser(String username, String serviceName, String roleName) {
        log.info("当前分支事务ID为: {}", RootContext.getXID());
        Integer userId = saveUser(UserVo.builder().username(username).serviceName(serviceName).enabled(1).build());
        Role role = iRoleService.getOne(new QueryWrapper<Role>().eq("name", roleName));
        if (role == null) {
            throw new AuthException("角色不存在");
        }
        iUserRoleService.save(UserRole.builder().uid(userId).rid(role.getId()).build());
    }

    @Transactional
    @Override
    public void deleteUser(String username, String serviceName) {
        log.info("当前分支事务ID为: {}", RootContext.getXID());
        User user = getOne(new QueryWrapper<User>().eq("username", username).eq("service_name", serviceName));
        if(user==null){
            return;
        }
        removeById(user);
        iUserRoleService.remove(new QueryWrapper<UserRole>().eq("uid",user.getId()));
    }
}
